package com.lookme.lmtool.cracker;

import com.lookme.lmtool.cracker.filter.IFilter;
import com.lookme.lmtool.model.ItemInfo;
import com.lookme.lmtool.model.ProjectInfo;
import com.lookme.lmtool.model.SpliterInfo;
import com.lookme.lmtool.utils.EnvUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class CrackerDebug {
    public static String ocr(String projectPath,String itemId, BufferedImage bufferedImage,String path){
        try {
            ImageIO.write(bufferedImage, "png", new File(path + "/source.png"));
            ProjectInfo projectInfo = EnvUtils.loadProject(projectPath);
            for (ItemInfo itemInfo : projectInfo.getItemInfoList()) {
                if (itemInfo.getId().equals(itemId)) {
                    return Cracker.cracker(itemInfo, bufferedImage, true, true, new CrackerListener() {
                        @Override
                        public void filterFinish(BufferedImage targetBuffedImage) {
                            try {
                                ImageIO.write(targetBuffedImage, "png", new File(path + "/target.png"));
                                for(IFilter filter:itemInfo.getFilterList()){
                                    ImageIO.write(filter.getImage(), "png", new File(path + "/"+filter.getName()+".png"));
                                }
                            } catch (Exception e) {
                            }
                        }
                        @Override
                        public void splitFinish(List<SpliterInfo> splitInfolist) {
                            try {
                                int i=0;
                                for(SpliterInfo spliterInfo:splitInfolist){
                                    i++;
                                    ImageIO.write(spliterInfo.getImage(), "png", new File(path + "/split"+i+"-"+spliterInfo.getOcrData()+".png"));
                                }
                            } catch (Exception e) {
                            }
                        }
                    });
                }
            }
        }catch(Exception e){

        }
        return null;
    }
}
